//
//  CZEmoctionCollectionViewCell.swift
//  WeiBO28
//
//  Created by 飞奔的羊 on 16/5/20.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
//表情视图 Cell
class CZEmoctionCollectionViewCell: UICollectionViewCell {
	//保存表情模型
	private lazy var emoticonButtons: [CZEmoticonButton] = [CZEmoticonButton]()
	var emicons: [CZEmoticon]? {
		didSet {
			guard let ets = emicons else{
			return
			}
			//去除重用问题
			for value in emoticonButtons {
			//隐藏所有表情按钮
				value.hidden = true
			}
			for (i,value) in ets.enumerate()
			{
				//取到 Button
				let button = emoticonButtons[i]
				button.emoticon = value
		
			}
		}
	}
	var indexPath: NSIndexPath? {
		didSet{
			guard let index = indexPath else {
			return
			}
			messageLabel.text = "显示第\(index.section)组"
		}
	}
	// MARK: ------懒加载视图----
	//显示 indexPathlabel
	private lazy var messageLabel: UILabel = {
	let label = UILabel(textColor: UIColor.orangeColor(), fontSIze: 30)
		return label
	}()
	override init(frame: CGRect) {
		super.init(frame: frame)
	setUpUI()
		
	}
	private lazy var deleteEmticonButton: UIButton = {
	let button = UIButton()
		button.setImage(UIImage(named: "compose_emotion_delete"), forState: .Normal)
		button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: .Highlighted)
		button.addTarget(self, action: #selector(CZEmoctionCollectionViewCell.deleteEmoticonClick), forControlEvents: .TouchUpInside)
return button
	}()
	required init?(coder aDecoder: NSCoder) {
		fatalError("init(coder:) has not been implemented")
	}
	
	private func setUpUI() {
	addChildButton()
		contentView.addSubview(deleteEmticonButton)
		
//	contentView.backgroundColor = RandomColor()
//		contentView.addSubview(messageLabel)
//		messageLabel.snp_makeConstraints { (make) in
//			make.center.equalTo(contentView)
//		}
	}
	//添加表情控件
	private func addChildButton() {
	//循环遍历20个表情控件
	 for _ in 0..<20
		{
			let button = CZEmoticonButton()
//			button.backgroundColor = RandomColor()
			//添加到 contenview
			button.addTarget(self, action: #selector(CZEmoctionCollectionViewCell.emoticonClick(_:)), forControlEvents: .TouchUpInside)
			button.titleLabel?.font = UIFont.systemFontOfSize(34)
//			button.backgroundColor = RandomColor()
			contentView.addSubview(button)
			
			//添加到表情数组中
			emoticonButtons.append(button)
		}
	}
	
	// MARK: ------点击事件------
	
	@objc private func deleteEmoticonClick() {
	//点击删除按钮发送通知
		NSNotificationCenter.defaultCenter().postNotificationName(DidSeletedDeleteEmoticonButtonNotification, object: nil)
	
	}
	
	@objc private func emoticonClick(button: CZEmoticonButton) {
		let emoticon = button.emoticon
		
	NSNotificationCenter.defaultCenter().postNotificationName(DidSeletedEmoticonNotification, object: emoticon)
	
	}
	override func layoutSubviews() {
		super.layoutSubviews()
		//按钮的宽度
		let itemWidth = width / 7
		//按钮的高度
		let itemHeight = height / 3
		
		
		//设置表情按钮的 frame
		//遍历数组, i 代表数组的下标
		for (i, value) in emoticonButtons.enumerate() {
			//按钮的大小
		value.size = CGSize(width: itemWidth, height: itemHeight)
		// 计算列的索引
			let colIndex = i % 7
			//计算行的索引
			let rowIndex = i / 7
			//设置 x 坐标
			value.x = CGFloat(colIndex) * itemWidth
			//设置 y 的坐标
			value.y = CGFloat(rowIndex) * itemHeight
			
			
		}
		//设置删除按钮的 frame
		deleteEmticonButton.size = CGSize(width: itemWidth, height: itemHeight)
		deleteEmticonButton.x = width - itemWidth
		//设置y 坐标
		deleteEmticonButton.y = height - itemHeight
	}
	
}















